package org.wishwork.api.inventory.count;

import cn.hutool.core.util.StrUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.api.ExtendBase;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

/**
 * @description: 库存盘点查询请求体
 * @author: WangYX
 * @create: 2022-04-06 17:47
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@ToString
@Getter
public class InventoryCountRequest extends ExtendBase {

    /**
     * 非必填
     * 指定盘点单号，多个用逗号分隔，最多50，和时间段不能同时为空
     */
    private String ioIds;

    /**
     * 非必填
     * 状态;WaitConfirm:待确认,Confirmed:生效,Archive:归档,Cancelled:取消,Delete:作废
     */
    private String status;

    private InventoryCountRequest(Integer pageIndex, Integer pageSize, String modifiedBegin, String modifiedEnd, String ioIds, String status) {
        super(pageIndex, pageSize, modifiedBegin, modifiedEnd);
        this.ioIds = ioIds;
        this.status = status;
    }

    @Override
    public Builder toBuilder() {
        return new Builder().pageIndex(this.pageIndex).pageSize(this.pageSize)
                .modifiedBegin(this.modifiedBegin).modifiedEnd(this.modifiedEnd)
                .ioIds(this.ioIds).status(this.status);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder extends ExtendBase.Builder<Builder, InventoryCountRequest> {

        private String ioIds;

        private String status;

        @Override
        public Builder modifiedBegin(String modifiedBegin) {
            this.modifiedBegin = modifiedBegin;
            return this;
        }

        @Override
        public Builder modifiedEnd(String modifiedEnd) {
            this.modifiedEnd = modifiedEnd;
            return this;
        }

        @Override
        public Builder pageIndex(Integer pageIndex) {
            this.pageIndex = pageIndex;
            return this;
        }

        @Override
        public Builder pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder ioIds(String ioIds) {
            String[] split = ioIds.split(",");
            if (split != null && split.length > 50) {
                throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "盘点单号，多个用逗号分隔，最多50!");
            }
            this.ioIds = ioIds;
            return this;
        }

        public Builder status(String status) {
            this.status = status;
            return this;
        }

        @Override
        public InventoryCountRequest build() {
            this.pageIndex = this.pageIndex == null ? 1 : this.pageIndex < 0 ? 1 : this.pageIndex;
            this.pageSize = this.pageSize == null ? 30 : this.pageSize < 0 ? 30 : this.pageSize;
            if (StrUtil.isEmpty(this.ioIds)) {
                keyIsEmptyTimeHandle(7);
            }
            modifiedTimeHandle(7);
            return new InventoryCountRequest(this.pageIndex, this.pageSize, this.modifiedBegin, this.modifiedEnd, this.ioIds, this.status);
        }
    }

}
